﻿#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using Cysharp.Threading.Tasks;
using Neatly.Module;
using UnityEngine;
using UnityEngine.Video;

namespace Neatly.Load.Manager
{
    public class VideoLoadManager : Singleton<VideoLoadManager>
    {
        private LoadModule m_LoadModule;
        private AssetBundle m_VideoBundle;

        public void Init(LoadModule loadModule)
        {
            m_LoadModule = loadModule;
        }

        public async UniTask<VideoClip> LoadVideo(string fileName)
        {
            if (NeatlyConfig.AssetBundleEnable)
            {
                if (m_VideoBundle != null)
                {
                    NeatlyConfig.AssetBundleUnloadAction(m_VideoBundle, true);
                }
                var loader = await m_LoadModule.LoadAssetBundleDirect(NeatlyConfig.VIDEO_PACKAGE);
                m_VideoBundle = loader.ResultObject as AssetBundle;
                return m_VideoBundle.LoadAsset<VideoClip>(fileName);
            }
            return await m_LoadModule.LoadAssetFileSync<VideoClip>( $"data/{NeatlyConfig.VIDEO_PACKAGE}/{fileName}{NeatlyConfig.SUFFIX_VIDEO}");
        }
    }
}